//
//  NetworkManager.swift
//  weibo
//
//  Created by tomato on 2021/9/28.
//

import AFNetworking

// 请求方法枚举
enum RequestMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class NetworkAFTools: AFHTTPSessionManager{
    //单例的创建,let是线程安全的
    static let sharedInstance: NetworkAFTools = {
        let tools = NetworkAFTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        return tools;
    }()//闭包
}

// MARK:- 封装网络请求方法
extension NetworkAFTools {
    /// 网络请求封装
    /// - Parameters:
    ///   - method: 请求方式
    ///   - URLString: URL
    ///   - parameters: 参数
    ///   - finished: 请求结果
    func request(method: RequestMethod,
              urlString: String,
             parameters: Any?,
               finished:@escaping (_ reslut:Any?, _ error:Error?) -> ()){
        if method == RequestMethod.GET {
            get(urlString, parameters: parameters, headers:nil) { press in
            } success: {task, resultData in
                finished(resultData,nil)
            }failure: { task, error in
                finished(nil,error)
            }
        }else{
            post(urlString, parameters: parameters, headers:nil) { press in
            } success: { task, resultData in
                finished(resultData,nil)
            }failure: { task, error in
                finished(nil,error)
            }
        }
    }
}

